package com.blb.community.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.blb.community.entity.SysUser;
import com.blb.community.service.ISysUserService;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Author 帅帅
 * @Date 2022/7/6 11:38
 * @PackageName:com.blb.community.handler
 * @ClassName: MyMetaObjectHandler
 * @Description: TODO
 * @Version 1.0
 */

/**
 * 对时间创建与修改时间的处理
 *
 * @author 帅帅
 */

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public void insertFill(MetaObject metaObject) {
        System.out.println(LocalDateTime.now());
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("roomStatus", "Auditing", metaObject);
        this.setFieldValByName("ownerPassword", bCryptPasswordEncoder.encode("123"), metaObject);

        this.setFieldValByName("createBy", SecurityContextHolder.getContext().getAuthentication().getName(), metaObject);
        this.setFieldValByName("updateBy", SecurityContextHolder.getContext().getAuthentication().getName(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateBy", SecurityContextHolder.getContext().getAuthentication().getName(), metaObject);
    }
}
